// pages/pay/pay.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: "",
    orderId: "",
    paytype: 2,
    lookUrl: "",
    url: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //  0 代支付 1是定金订单
    console.log(options)
    var _this = this
    if (options.state == 1) {
      this.setData({
        paytype: 3
      })
    }
    this.setData({
      orderId: options.orderId,
      orderState: options.state
    })
    wx.getStorage({
      key: 'openid',
      success(res) {
        _this.setData({
          openid: res.data
        })
      }
    })
    this.getOrderInfo(options.orderId)
  },
  onShow: function() {
    wx.hideLoading()
  },
  donePay: function() {

    var _this = this
    wx.showLoading({
      title: '支付中...',
    })
    wx.request({
      url: 'https://miniapp.odeman.com/pay/payfee.php',
      data: {
        orderid: _this.data.orderId,
        openid: _this.data.openid,
        paytype: _this.data.paytype
      },
      method: "post",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        wx.hideLoading()
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: 'MD5',
          paySign: res.data.paySign,
          success(res) {
            wx.navigateTo({
              url: '/pages/order_list/order_list?type=5'
            })
          },
          fail(err) {
            console.log(err)
          }
        })

      }
    })
  },
  radioChange: function(e) {
    console.log(e)
    this.setData({
      paytype: e.detail.value
    })
  },

  getOrderInfo: function(orderId) {
    var _this = this
    wx.request({
      url: 'https://miniapp.odeman.com/pay/index.php',
      data: {
        itemid: orderId
      },
      method: "post",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        var list = res.data.list
        console.log(res.data.list.url)
        wx.setStorageSync('templateUrl', res.data.list.url)
        _this.setData({
          hdmc: list.hdmc,
          price: list.price,
          frometime: list.frometime,
          adddate: list.adddate,
          dinjing: list.dinjing,
          stu_name: list.stu_name,
          sy_money: list.sy_money,
          totime: list.totime,
          pay_money: list.pay_money

        })

      }
    })
  },

  template: function(e) {
    wx.showLoading({
      title: '加载中'
    })
    wx.navigateTo({
      url: '/pages/contractTemplate/contractTemplate'
    })
  }

})